package cn.rkylin.oms.goods.util.handler;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.context.support.SpringBeanAutowiringSupport;

import com.greenpineyu.fel.common.StringUtils;

import cn.rkylin.oms.goods.util.request.Request;
import cn.rkylin.oms.order.service.IOrderService;
import cn.rkylin.oms.warehouse.stockGoods.service.IStockGoodsService;

public abstract class SnOpenableValidateHandler extends SpringBeanAutowiringSupport{
    /**
     * stockGoodsService:库存查询服务.
     */
    @Autowired
    protected IStockGoodsService stockGoodsService;
    
    /**
     * MESSAGE:未完成单据的统一提示信息.
     */
    protected String UNGOING_RECEPTION_MESSAGE = "该系统商品存在有效的未完成的在途单据，不允许变更 ";
    
    /**
     * orderService:订单查询服务.
     */
    @Autowired
    protected IOrderService orderService;
    
    protected SnOpenableValidateHandler successor;
    
    public void setSuccessor(SnOpenableValidateHandler handler){
        this.successor = handler;
    }
    
    /**
     * handleRequest:处理请求，并返回错误信息列表. <br/>
     *
     * @author zhanghao
     * @param request
     * @return 错误信息列表
     */
    public List<String> handleRequest(Request request) {
        List<String> errorMessageList = new ArrayList<String>();
        try {
            if(isValid(request)) {
                if(null!=this.successor) {
                    return successor.handleRequest(request);
                }
            }else {
                String errorMessage = getMessage();
                if(StringUtils.isNotEmpty(errorMessage)) {
                    errorMessageList.add(errorMessage);
                }
            }
        }catch(Exception e) {
            errorMessageList.add(e.getMessage());
        }
        return errorMessageList;
    }
    
    public boolean isValid(Request request) throws Exception {
        Map<String,String> paramMap = new HashMap<String,String>();
        paramMap.put("prjId", request.getGoodsInfo().getPrjId());
        paramMap.put("goodsId", request.getGoodsInfo().getGoodsId());
        int unGoingCount= getUnGoingCount(paramMap);
        if(unGoingCount==0) {
            return true;
        }else {
            return false;
        }
    }

    public abstract int getUnGoingCount(Map<String, String> paramMap) throws Exception;

    public abstract String getMessage();
}
